package com.unipay.oss;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Project: yitong_parent
 * @Author: Chenmy
 * @Time: 2023-10-10 12:54:39
 * @Desc: TODO
 */

public class UploadController {
    public static void main(String[] args) {
        //获取S3Client对象
        AWSCredentials awsCredentials = new BasicAWSCredentials("DV2PJ96K8EZZ03OMUHPZ", "ndHnlh0Xoa1RxEeJyeaGIlyv1TPhxYfpp1hQp2C5");
        AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials));
        //builder.setRegion(region);
        AmazonS3 s3Client = builder.build();

        //开始上传文件
        //s3FileFullPath作为该文件的唯一标识符key
        s3Client.putObject("ts-bdcs", "/sftp/provinceBusiness", new File("test.txt"));
    }
    private String accessKeyID ="accessKeyID";

    private String secretKey = "secretKey";
    //S3上桶的名称
    private String bucketName = "bucketName";
    //所属地区
    private String region = "region";

    /**
     * 上传文件
     * @param multipartFile
     * @return
     * @throws Exception
     */
    @RequestMapping("/uploadFile")
    public String uploadFile(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        if (multipartFile.isEmpty()) {
            return "上传文件不能为空";
        }
        //生成随机文件名
        String localFileName = UUID.randomUUID() + "." + multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);
        Date date = new Date();
        SimpleDateFormat formatter_yyyy = new SimpleDateFormat("yyyy");
        SimpleDateFormat formatter_MM = new SimpleDateFormat("MM");
        //在随机名前加上年月
        String s3FileFullPath = formatter_yyyy.format(date) + "/" + formatter_MM.format(date) + "/" + localFileName;

        //获取S3Client对象
        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKeyID, secretKey);
        AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials));
        builder.setRegion(region);
        AmazonS3 s3Client = builder.build();

        ObjectMetadata metadata  = new ObjectMetadata();
        metadata.setContentType(multipartFile.getContentType());
        metadata.setContentLength(multipartFile.getSize());
        try {
            //开始上传文件
            //s3FileFullPath作为该文件的唯一标识符key
            PutObjectResult putResult = s3Client.putObject(bucketName, s3FileFullPath, multipartFile.getInputStream(), metadata);
            System.out.println("【流方式】上传MumbaiS3完成，md5：" + putResult.getETag() + "，S3文件：" + s3FileFullPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回文件在S3上的key
        return s3FileFullPath;
    }

}
